package com.aabte.nettyleran.netty.serial.protobuf.proto;

import com.google.protobuf.InvalidProtocolBufferException;

/**
 * @author Daniel
 * @version 1.0
 * @date 2020/3/14
 */
public class TestSubscribeReqProto {

    public static void main(String[] args) throws InvalidProtocolBufferException {
        SubscribeReqProto.SubscribeReq req = createSubscribeReq();
        System.out.println("Before encode: \n" + req);

        SubscribeReqProto.SubscribeReq req2 = decode(encode(req));
        System.out.println("After decode: \n" + req2);

        System.out.println("Assert equal: " + req2.equals(req));
    }

    private static byte[] encode(SubscribeReqProto.SubscribeReq req) {
        return req.toByteArray();
    }

    private static SubscribeReqProto.SubscribeReq decode(byte[] body) throws InvalidProtocolBufferException {
        return SubscribeReqProto.SubscribeReq.parseFrom(body);
    }

    private static SubscribeReqProto.SubscribeReq createSubscribeReq() {
        return SubscribeReqProto.SubscribeReq.newBuilder()
                .setSubReqId(1)
                .setUsername("tom")
                .setProductName("Netty Book")
                .setAddress("lata")
                .build();
    }

}
